﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Input.StylusPlugIns;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace SharpSoft.WPF.Controls
{

    public class StrokeCanvas : InkCanvas
    {
        public StrokeCanvas()
        {
            var render = new CalligrapyStrokeRender();
            this.Background = Brushes.WhiteSmoke;
            this.DefaultDrawingAttributes = new DrawingAttributes() { Width = 10, Height = 10, Color = Colors.Black, IsHighlighter = false, FitToCurve = true, StylusTip = StylusTip.Ellipse };
        }



        protected override void OnStrokeCollected(InkCanvasStrokeCollectedEventArgs e)
        {
            if (this.PenType == PenType.Calligraphy)
            { //软笔替换笔迹
                this.Strokes.Remove(e.Stroke);
                this.Strokes.Add(new ExStroke(e.Stroke.StylusPoints, this.DefaultDrawingAttributes));
            }
        }
        /// <summary>
        /// 将当前笔迹内容获取为图片
        /// </summary>
        /// <returns></returns>
        public RenderTargetBitmap GetRenderTargetBitmap()
        {
            return VisualHelper.Capture(this);
        }

        /// <summary>
        /// 清空笔迹
        /// </summary>
        public void ClearStrokes()
        {
            this.Strokes.Clear();
        }
        /// <summary>
        /// 后退一步
        /// </summary>
        public void BackStep()
        {
            if (this.Strokes.Count > 0)
            {
                this.Strokes.RemoveAt(this.Strokes.Count - 1);
            }
        }

        #region 鼠标事件处理

        protected override void OnPreviewKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);
            if (e.Key == Key.Delete)
            {
                this.ClearStrokes();
            }
        }
        #endregion

        #region 渲染笔画


        public PenType PenType
        {
            get { return (PenType)GetValue(PenTypeProperty); }
            set { SetValue(PenTypeProperty, value); }
        }

        public static readonly DependencyProperty PenTypeProperty =
            DependencyProperty.Register("PenType", typeof(PenType), typeof(StrokeCanvas), new PropertyMetadata(PenType.Hardtipped));



        public double StrokeWeight
        {
            get { return (double)GetValue(StrokeWeightProperty); }
            set { SetValue(StrokeWeightProperty, value); }
        }

        public static readonly DependencyProperty StrokeWeightProperty =
            DependencyProperty.Register("StrokeWeight", typeof(double), typeof(StrokeCanvas), new PropertyMetadata(10d, new PropertyChangedCallback((obj, e) =>
            {
                var sc = obj as StrokeCanvas;
                sc.DefaultDrawingAttributes.Width = (double)e.NewValue;
                sc.DefaultDrawingAttributes.Height = (double)e.NewValue;
            })));



        public Color StrokeColor
        {
            get { return (Color)GetValue(StrokeColorProperty); }
            set { SetValue(StrokeColorProperty, value); }
        }

        public static readonly DependencyProperty StrokeColorProperty =
            DependencyProperty.Register("StrokeColor", typeof(Color), typeof(StrokeCanvas), new PropertyMetadata(Colors.Black, new PropertyChangedCallback((obj, e) =>
            {
                var sc = obj as StrokeCanvas;
                sc.DefaultDrawingAttributes.Color = (Color)e.NewValue;
            })));



        #endregion


    }
    /// <summary>
    /// 笔类型
    /// </summary>
    public enum PenType
    {
        /// <summary>
        /// 硬笔
        /// </summary>
        Hardtipped,
        /// <summary>
        /// 软笔
        /// </summary>
        Calligraphy
    }
}
